package com.badibadi.uniclubber;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.activity.BaseActivity;
import com.badibadi.adapter.GridViewListAdapter;
import com.badibadi.infos.MailXiangXiModel;
import com.badibadi.infos.Results;
import com.badibadi.mail.Mail245Activity;
import com.badibadi.mytools.CommonUtils;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.HtmlUtils;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.RegexUtils;
import com.badibadi.mytools.Utils;
import com.lidroid.xutils.util.LogUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

/**
 * 接受俱乐部通知的activity
 * 
 * @author Administrator
 * 
 */
public class AcceptClubMailActivity extends BaseActivity {

	protected static final String TAG = "AcceptClubMailActivity";
	private String zhanneixinId;// 站内信的id
	private String ClubName;// 俱乐部名称
	public MailXiangXiModel mailXiangXiModel;
	public Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(AcceptClubMailActivity.this);
				try {
					Utils.showMessage(AcceptClubMailActivity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
				}
				break;
			case 2:
				Utils.ExitPrgress(AcceptClubMailActivity.this);
				try {
					// initListView();
					initView();
				} catch (Exception e) {
				}
				break;
			case 3:
				Utils.ExitPrgress(AcceptClubMailActivity.this);
				try {
					Utils.showMessage(AcceptClubMailActivity.this,
							getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
				}
				break;
			case 4:// 系统错误
				Utils.ExitPrgress(AcceptClubMailActivity.this);
				try {
					Utils.showMessage(AcceptClubMailActivity.this,
							getResources().getString(R.string.SystemError));
				} catch (Exception e) {
				}
				break;
			case 5:// 删除站内信成功
				Utils.ExitPrgress(AcceptClubMailActivity.this);
				try {
					Utils.showMessage(AcceptClubMailActivity.this,
							getResources().getString(R.string.l_xb55));
					Constants.is_shuaxin=true;
					finish();
				} catch (Exception e) {
				}
				break;
			case 6:// 删除站内信s失败
				Utils.ExitPrgress(AcceptClubMailActivity.this);
				try {
					Utils.showMessage(AcceptClubMailActivity.this,
							getResources().getString(R.string.l_xb113));
				} catch (Exception e) {
				}

				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_accept_message);
		init();

	}

	protected void init() {
		// TODO Auto-generated method stub
		Constants.is_shuaxin=false;
		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		zhanneixinId = getIntent().getStringExtra("zhanneixinId");
		// zhanneimsgId = getIntent().getStringExtra("zhanneimsgId");
		ClubName = getIntent().getStringExtra("ClubName");
		((TextView) (this.findViewById(R.id.jijianfang)))
				.setText("" + ClubName);
		((TextView) (this.findViewById(R.id.shoujianfang))).setText(""
				+ Utils.getu_Name(AcceptClubMailActivity.this));

		showClubNotice(Utils.getUid(AcceptClubMailActivity.this), zhanneixinId);
		this.findViewById(R.id.delete).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						delete(Utils.getUid(AcceptClubMailActivity.this),
								zhanneixinId, "zhannei_clubnotice", "", handler);
					}
				});
	}

	/**
	 * 初始化view
	 */
	protected void initView() {
		// TODO Auto-generated method stub
		TextView content = (TextView) this.findViewById(R.id.content);
		// content.setText();
		GridView item_gridView = (GridView) this.findViewById(R.id.item);
		// 获取所有的包含图片的<src= >类似的html语言
		List<String> list = null;
		if (mailXiangXiModel.getContents() != null) {
			list = RegexUtils.getImgSrc(mailXiangXiModel.getContents());
//			for (int i = 0; i < list.size(); i++) {
//				LogUtils.d("列表是：==》" + list.get(i).toString());
//			}

		}
		if (list != null && !list.isEmpty()) {
//			LogUtils.d("列表是：==》不是空");
			GridViewListAdapter adapter = new GridViewListAdapter(
					AcceptClubMailActivity.this, list);
			item_gridView.setAdapter(adapter);
			String aa = mailXiangXiModel.getContents();

			// for (int i = 0; i < list.size(); i++) {
			//
			// // <img src=' ' />
			// aa = aa.replace("<img src='" + list.get(i) + "' />", "");
			// }
			List<String> listgroup = RegexUtils.getImgSrcGroup(aa);
			for (int i = 0; i < listgroup.size(); i++) {
				// <img src=' ' />
				if(listgroup.get(i).indexOf(Constants.Fei_biaoqing_qianzui)!=-1 || list.get(i).indexOf(Constants.Fei_biaoqing_qianzui2)!=-1 || list.get(i).indexOf(Constants.Fei_biaoqing_qianzui3)!=-1 || list.get(i).indexOf(Constants.Fei_biaoqing_qianzui4)!=-1)
				aa = aa.replaceAll(listgroup.get(i), "");// ("<img src='"+list.get(i)+"' />","");

			}
			new HtmlUtils(AcceptClubMailActivity.this, content, aa)
					.showTextView();
//			LogUtils.d("aa=" + aa);
			// <img src=' '/ >

		} else if (list == null || list.isEmpty()) {
//			LogUtils.d("列表是：==》是空");
			new HtmlUtils(AcceptClubMailActivity.this, content,
					mailXiangXiModel.getContents()).showTextView();
		}

		// new HtmlUtils(AcceptClubMailActivity.this,content,
		// mailXiangXiModel.getContents(),180).showTextView();

	}

	/**
	 * 查看站内信之"俱乐部通知"接口---POST
	 * 
	 * @param $uid
	 *            ---登录用户ID
	 * @param $zhanneiId
	 *            ---俱乐部通知消息ID zhannei_clubnotice
	 */
	protected void showClubNotice(final String uid, final String zhanneiId) {
		Utils.showPrgress(AcceptClubMailActivity.this);

		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				map.put("zhanneiId", zhanneiId);

				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "inform/showClubNotice");
				if (content != null) {
					//    ("返回的投票数据是:" + content);
					Results results3 = Utils.checkResult_NNN(
							AcceptClubMailActivity.this, content);
					if (results3 != null
							&& !results3.getRetmsg().equals("null")) {

						try {
							mailXiangXiModel = JSONUtils.getEntityByJsonString(
									results3.getRetmsg(),
									MailXiangXiModel.class);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}

						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	/**
	 * 删除站内信操作---POST
	 * 
	 * @param $uid
	 *            ---登录用户ID
	 * @param $zhanneiId
	 *            ----站内信ID
	 * @param $type
	 *            ----删除的站内信类型 zhannei_usercontact：用户联系 zhannei_clubnotice：俱乐部通知
	 *            zhannei_contactclub：联系俱乐部
	 * @param $rank
	 *            ---操作者的身份 asker：问方 answerer：答方 注意：当为“用户联系”才有该参数
	 */
	public void delete(String uid, String zhanneiId, String type, String rank,
			android.os.Handler handler) {
		Utils.showPrgress(AcceptClubMailActivity.this);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("zhanneiId", zhanneiId);
		map.put("type", type);
		if (type.equals("zhannei_usercontact")) {
			map.put("rank", rank);
		}
		CommonUtils.CommomPostfunction(AcceptClubMailActivity.this,
				Constants.BadiUrl_1 + "/" + "inform/delete", map, handler, 1,
				4, 5, 6);
	}

}
